package com.codebyorder.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.codebyorder.entity.Category;
import com.codebyorder.mapper.CategoryMapper;
import com.codebyorder.req.CategoryQueryReq;
import com.codebyorder.resp.CategoryQueryResp;
import com.codebyorder.resp.PageResp;
import com.codebyorder.service.CategoryService;
import com.codebyorder.util.CopyUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
* @author 29038
* @description 针对表【category(分类)】的数据库操作Service实现
* @createDate 2022-12-12 09:00:48
*/
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category>
    implements CategoryService{
    @Resource
    private CategoryMapper categoryMapper;
    private static final Logger LOG = LoggerFactory.getLogger(CategoryServiceImpl.class);


    @Override
    public PageResp<CategoryQueryResp> categoryToResp(CategoryQueryReq categoryQueryReq) {
        PageHelper.startPage(categoryQueryReq.getPage(), categoryQueryReq.getSize());//和第一个查询语句放在一起,请求参数
        List<Category> categorys = categoryMapper.selectByName(categoryQueryReq.getName());

        PageInfo<Category> pageInfo = new PageInfo<>(categorys);
        LOG.info("总行数:{}",pageInfo.getTotal());//返回给前端
        LOG.info("总页数:{}",pageInfo.getPages());

        List<CategoryQueryResp> categoryResp = CopyUtil.copyList(categorys, CategoryQueryResp.class);
        PageResp<CategoryQueryResp> pageResp = new PageResp<>();
        pageResp.setTotal(pageInfo.getTotal());
        pageResp.setList(categoryResp);

        return pageResp;
    }
}




